﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using MapPanel;

namespace CityLamp
{
    public partial class NodeListControl : UserControl
    {
        private Hashtable ID_Index_HT = new Hashtable();
        private object listViewLock = new object();

        private List<MapPanel.StreetLamp> m_StreetLampList;
        public List<MapPanel.StreetLamp> StreetLampList
        {
            get
            {
                return this.m_StreetLampList;
            }
            set
            {
                this.m_StreetLampList = value;
            }
        }
        public NodeListControl()
        {
            InitializeComponent();
        }

        private void NodeListControl_Load(object sender, EventArgs e)
        {
            nodeListView.View = System.Windows.Forms.View.Details;
            nodeListView.CheckBoxes = true;
            nodeListView.HideSelection = false;
            nodeListView.FullRowSelect = true;

            //nodeListView.Columns.Add("Header", "");
            //nodeListView.Columns[0].Width = 10;
            int width = nodeListView.Width;
            nodeListView.Columns.Add("NodeID", "节点ID");
            int w1 = nodeListView.Columns["NodeID"].Width = (int)(0.25 * width);

            nodeListView.Columns.Add("PlaceName", "安装位置");
            int w2 = nodeListView.Columns["PlaceName"].Width = (int)(0.35 * width);

            nodeListView.Columns.Add("Status", "执行结果");
            nodeListView.Columns["Status"].Width = (int)(width - w1 - w2 - 22);
        }

        public void UpdateNodeListView()
        {
            this.nodeListView.Items.Clear();
            int index = 0;
            if (m_StreetLampList != null && m_StreetLampList.Count > 0)
            {
                foreach(MapPanel.StreetLamp streetLamp in this.m_StreetLampList)
                {
                    if (streetLamp.Online)
                    {
                        ID_Index_HT.Add(streetLamp.LampId, index++);
                        ListViewItem row = new ListViewItem();
                        row.SubItems[0].Text = streetLamp.LampId.ToString();
                        //row.SubItems.Add(streetLamp.LampId.ToString());
                        row.SubItems.Add(streetLamp.StreetName);
                        row.SubItems.Add("");
                        this.nodeListView.Items.Add(row);
                    }
                }
            }
        }

        public List<MapPanel.StreetLamp> GetSelectedLampList()
        {
            int index = 0;
            List<MapPanel.StreetLamp> lampListSelected = new List<MapPanel.StreetLamp>();
            foreach (MapPanel.StreetLamp streetLamp in this.m_StreetLampList)
            {
                if (streetLamp.Online)
                {
                    if (nodeListView.Items[index].Checked == true)
                        lampListSelected.Add(streetLamp);
                    index++;
                }
            }
            return lampListSelected;
        }

        public void ClearNodeListViewResult()
        {
            foreach(ListViewItem item in this.nodeListView.Items)
            {
                item.SubItems[2].Text = "";
            }
        }

        public void SetResultAsSuccess(int lampId)
        {
            try
            {
                if (ID_Index_HT.Contains(lampId))
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        lock (listViewLock)
                        {
                            this.nodeListView.Items[(int)ID_Index_HT[lampId]].SubItems[2].Text = "成功";
                        }
                    }));
                }
            }
            catch
            {
            }
        }

        public void SetResultAsFailed()
        {
            try
            {
                foreach(ListViewItem item in this.nodeListView.Items)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        if (item.SubItems[2].Text != "成功")
                        {
                            lock (listViewLock)
                            {
                                item.SubItems[2].Text = "失败";
                            }
                        }
                    }));
                }
            }
            catch
            {
            }
        }
    }
}
